package com.gbx.ximalayating.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.fragment.DownloadTingFragment;
import com.gbx.ximalayating.fragment.FindFragment;
import com.gbx.ximalayating.fragment.MineFragment;
import com.gbx.ximalayating.fragment.SubscriptionTingFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioGroup;
    private FragmentManager manager;
    private List<Fragment> list;
    private FindFragment findFragment;
    private SubscriptionTingFragment subscriptionTingFragment;
    private DownloadTingFragment downloadTingFragment;
    private MineFragment mineFragment;
    private int[] fragmentId=new int[]{R.id.find_btn,R.id.subscriptionTing_btn,R.id.downloadTing_btn,R.id.me_btn,R.id.video_play_btn};
    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.main_radioGroup);
        radioGroup.setOnCheckedChangeListener(this);

        list =new ArrayList<>();
        findFragment = new FindFragment();
        subscriptionTingFragment = new SubscriptionTingFragment();
        downloadTingFragment = new DownloadTingFragment();
        mineFragment = new MineFragment();

        list.add(findFragment);
        list.add(subscriptionTingFragment);
        list.add(downloadTingFragment);
        list.add(mineFragment);
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i < list.size(); i++) {
            transaction.add(R.id.main_container, list.get(i));
//            transaction.hide(list.get(i));
        }
        transaction.commit();
        showFragment(0);
//        transaction.show(list.get(0));
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < fragmentId.length; i++) {
            if (fragmentId[i]==checkedId){
                if (i!=4){
                   showFragment(i);
                }else {
                    Intent intent=new Intent(this, PlayActivity.class);
                    startActivity(intent);
                }
            }
        }
    }
    public void showFragment(int position){
        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i < list.size(); i++) {
            if (position==i){
                transaction.show(list.get(i));
            }else {
                transaction.hide(list.get(i));
            }
        }
        transaction.commit();
    }

    @Override
    public void onBackPressed() {
        View view = LayoutInflater.from(this).inflate(R.layout.alert_dialog_exit, null);
        dialog =new AlertDialog.Builder(this).setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.super.onBackPressed();
                    }
                })
                .setNeutralButton("最小化", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        moveTaskToBack(true);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }
}
